<?php

namespace App\Models;

use App\Models\BaseModel;

class Goods extends BaseModel
{
    protected $table = 'goods';
    public $timestamps = false;

    const STATUS_OPEN = 1; // 上架
    const STATUS_CLOSE = 2; // 下架
    const STATUS = [
        self::STATUS_OPEN => '上架',
        self::STATUS_CLOSE => '下架'
    ];
//     售卖状态
    const SALE_STATUS_OPEN = 1;
    const SALE_STATUS_CLOSE = 2;
    const SALE_STATUS_OUT = 3;
    const SALE_STATUS = [
        self::SALE_STATUS_OPEN  => '销售中',
        self::SALE_STATUS_CLOSE => '已下架',
        self::SALE_STATUS_OUT => '已售罄'
    ];

//是否会员价
    const IS_SALE_IS = 1;//1会员价
    const IS_SALE_NO = 2;//2原价
    const IS_SALE = [
        self:: IS_SALE_IS =>'会员价',
        self:: IS_SALE_NO =>'原价',

    ];
//是否可用优惠券
    const IS_COUPON_IS = 1;//1可用
    const IS_COUPON_NO = 2;//2不可用
    const IS_COUPON = [
        self:: IS_SALE_IS =>'可用',
        self:: IS_SALE_NO =>'不可用',

    ];
//是否支持余额支付
    const IS_PAY_BALANCE_IS = 1;//1支持
    const IS_PAY_BALANCE_NO = 2;//2不支持
    const IS_PAY_BALANCE= [
        self:: IS_SALE_IS =>'余额支付',
        self:: IS_SALE_NO =>'不支持余额支付',

    ];

//    public function getIsPayBalanceAttribute($value)
//    {
//        return self::IS_PAY_BALANCE["$value"];
//    }
//    public function getIsSaleAttribute($value)
//    {
//        return self::IS_SALE["$value"];
//    }
//    public function getIsCouponAttribute($value)
//    {
//        return self::IS_COUPON["$value"];
//    }
//
    public function getPriceAttribute($value)
    {
        return round($value,2);
    }


    public function insert($data)
    {
        return $this->insertGetId($data);
    }

    public function edit($id, $data)
    {
        if($this->find($id)){
            return DB::table($this->table)->where('id', $id)->update($data);
        }
    }

    public function del($id)
    {
        return $this->where('id', $id)->delete();
    }

//    public function find($id)
//    {
//        return $this->where('id', $id)->first()->toArray();
//    }

    public function getAll()
    {
        return $this->get()->toArray();
    }

    public function brandInfo(){
        return $this->hasOne('App\Models\GoodsBrand','id','brand');
    }

    /**
     *  和模块的关联关系
     * @return $this
     */
    public function goodsHomeRelations()
    {
        return $this->belongsToMany('App\Models\GoodsHomeRelation', 'goods_home_relation', 'good_id', 'home_id')->withPivot(['good_id', 'home_id']);
    }

    public function orderGoods()
    {
        return $this->hasMany('App\Models\OrderGoods','sku','sku');
    }

    public function goodsBrand()
    {
        return $this->hasOne('App\Models\GoodsBrand','id','brand')->withDefault([
            'id'=>'',
            'name'=>'',
            'photo'=>'',
            'sort' =>0,
            'status'=>1,
        ]);
    }



}


